<cfcomponent extends="Controller" output="false">
	
	<cffunction name="init">
		<cfset filters(through="checkResults",only="new")>
	</cffunction>
	
	<cffunction name="checkResults" hint="Checks to see if a result already exists for a fixtureid">
		<cfset results = model("Result").findOne(where="FixtureID='#params.key#'")>
		<cfif isObject(results)>
			<cfset redirectTo(route="enterResults",action="edit",key=results.resultId,seasonid=params.seasonId)>
		</cfif>
	</cffunction>
	
	<!--- -results/index --->
	<cffunction name="index">
		<cfset results = model("Result").findAll()>
	</cffunction>
	
	<!--- -results/show/key --->
	<cffunction name="show">
		
		<!--- Find the record --->
    	<cfset result = model("Result").findByKey(params.key)>
    	
    	<!--- Check if the record exists --->
	    <cfif NOT IsObject(result)>
	        <cfset flashInsert(error="Result #params.key# was not found")>
	        <cfset redirectTo(action="index")>
	    </cfif>
			
	</cffunction>
	
	<!--- -results/new --->
	<cffunction name="new">
		<cfset result = model("Result").new()>
		<cfif StructKeyExists(params,"key")>
			<cfset result.fixtureId = params.key>
		</cfif>
		<cfif StructKeyExists(params,"seasonId")>
			<cfset result.seasonId = params.seasonId>
		</cfif>
	</cffunction>
	
	<cffunction name="scorecard">
		
		<cfset tmp = model("Result").getScorecard(params.key)>
		
		<cfset result = model("Result").findOne(where="FixtureID='#params.key#'")>
		
		
		<cfset playerresult = model("PlayerResult").findAll(where="FixtureID='#params.key#'")>
		
		<cfif playerresult.recordCount>
			<!--- Already got the mini-fixture, enter scores --->
			<cfset fixture = model("Fixture").findByKey(playerresult.fixtureid)>
			<cfset homeTeam = model("Team").findByKey(key=fixture.homeTeamId,select="TeamName,TeamID")>
			<cfif isObject(homeTeam)>
				<cfset homeTeamPlayers = model("TeamPlayer").findAll(where="TeamID='#homeTeam.teamId#'",include="Player",order="Player.Position")>
			</cfif>
			
			
			<cfset awayTeam = model("Team").findByKey(key=fixture.awayTeamId,select="TeamName,TeamID")>
			<cfif isObject(awayTeam)>
				<cfset awayTeamPlayers = model("TeamPlayer").findAll(where="TeamID='#awayTeam.teamId#'",include="Player",order="Player.Position")>
			</cfif>
						
		<cfelse>
			<!--- Need to generate mini-fixture --->
			<cfset fixture = model("Fixture").findByKey(params.key)>
			
			<cfif isObject(fixture)>
			
			
				<cfset homeTeam = model("Team").findByKey(key=fixture.homeTeamId,select="TeamName,TeamID")>
				<cfif isObject(homeTeam)>
					<cfset homeTeamPlayers = model("TeamPlayer").findAll(where="TeamID='#homeTeam.teamId#'",include="Player",order="Player.Position")>
				</cfif>
				
				<cfset awayTeam = model("Team").findByKey(key=fixture.awayTeamId,select="TeamName,TeamID")>
				<cfif isObject(awayTeam)>
					<cfset awayTeamPlayers = model("TeamPlayer").findAll(where="TeamID='#awayTeam.teamId#'",include="Player",order="Player.Position")>
				</cfif>
			</cfif>
			<cfset renderPage(action="generate")>
		</cfif>
	</cffunction>
	
	<!--- -results/edit/key --->
	<cffunction name="edit">
		<!--- Find the record --->
    	<cfset result = model("Result").findByKey(params.key)>
    	
    	<!--- Check if the record exists --->
	    <cfif NOT IsObject(result)>
	        <cfset flashInsert(error="Result #params.key# was not found")>
			<cfset redirectTo(action="index")>
	    </cfif>
		
	</cffunction>
	
	<!--- -results/create --->
	<cffunction name="create">
		<cfset result = model("Result").new(params.result)>

		<!--- Verify that the result creates successfully --->
		<cfif result.save()>
			<cfset flashInsert(success="The result was created successfully.")>
            <cfset redirectTo(controller="seasons",action="show",key=result.seasonId)>
		<!--- Otherwise --->
		<cfelse>
			<cfset flashInsert(error="Awnoess an error")>
			<cfset redirectTo(back=true)>
			<!---<cfset renderPage(action="new")>--->
		</cfif>
	</cffunction>
	
	<!--- -results/update --->
	<cffunction name="update">
		<cfset result = model("Result").findByKey(params.key)>
		
		<!--- Verify that the result updates successfully --->
		<cfif result.update(params.result)>
			<cfset flashInsert(success="The result was created successfully.")>
            <cfset redirectTo(controller="seasons",action="show",key=result.seasonId)>
		<!--- Otherwise --->
		<cfelse>
			<cfset flashInsert(error="Awnoess an error")>
			<cfset redirectTo(back=true)>
		</cfif>
	</cffunction>
	
	<!--- -results/delete/key --->
	<cffunction name="delete">
		<cfset result = model("Result").findByKey(params.key)>
		
		<!--- Verify that the result deletes successfully --->
		<cfif result.delete()>
			<cfset flashInsert(success="The result was deleted successfully.")>	
            <cfset redirectTo(controller="seasons",action="show",key=params.seasonId)>
		<!--- Otherwise --->
		<cfelse>
			<cfset flashInsert(error="There was an error deleting the result.")>
			<cfset redirectTo(action="index")>
		</cfif>
	</cffunction>
	
</cfcomponent>
